package com.useeinfo.asg.modules.security.biz;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.security.dao.SystemUserDao;
import com.useeinfo.asg.modules.security.entity.SystemUser;
import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Author: 居泽平
 */
@Service("systemUserBizWeb")
public class SystemUserBiz extends CrudBiz<SystemUserDao, SystemUser> {
	public List<SystemUser> queryUser(String jpql, Map<String, Object> params) {

		return dao.findUser(jpql, params);
	}
	public Boolean existUserAccount(SystemUser systemUser) {
		//若用户名为空
		logger.info("用户名为：" + systemUser.getUserAccount());
		if (StringUtils.isBlank(systemUser.getUserAccount())) {
			return true;
		}

		//根据入库单号查询
		QueryParam queryParam = new QueryParam();
		queryParam.getSqlMap().put("userAccount", systemUser.getUserAccount());
		List<SystemUser> systemUserList = findList(queryParam);

		if (systemUser.getId()== null) {
			//新增操作，判断列表size是否为0
			return CollectionUtils.isNotEmpty(systemUserList);
		} else {
			//修改操作，情况一：判断list长度是否为1，并且与自身相同，那么就是不存在，所以结果取反
			//修改操作，情况二：判断list长度是0
			return !((systemUserList.size() == 1 && systemUserList.get(0).getId().equals(systemUser.getId())) || systemUserList.size() == 0);
		}
	}

	@Transactional
	public SystemUser getByUserAccount(String userAccount) {

		if (StringUtils.isBlank(userAccount)) {
			return null;
		}

		QueryParam queryParam = new QueryParam();
		queryParam.getSqlMap().put("userAccount", userAccount);

		List<SystemUser> systemUserList = findList(queryParam);
		if (CollectionUtils.isEmpty(systemUserList)) {
			return null;
		}

		//查询结果串不为空，则返回第一个
		SystemUser systemUser = systemUserList.get(0);
		//logger.debug("systemRole:[{}]", systemUser.getSystemRole());

		return systemUser;
	}

	public List<SystemUser> findByRoleId(Long roleId) {
		return this.dao.findByRoleId(roleId);
	}

	@Transactional
	public void logicRemove(Long userId) {
		dao.logicRemove(userId);
	}
	@Transactional(readOnly = false)
	public JSONObject deleteId(Long id) {

		JSONObject retJson = CommUtil.jsonResult(false);
		SystemUser systemUser = null;
		if (CommUtil.null2Long(id) > 0l && ((systemUser = dao.findModel(CommUtil.null2Long(id))) != null)) {
			systemUser.setDeleteStatus(true);
			if (dao.update(systemUser) == 1) {
				retJson = CommUtil.jsonResult(true);
			}
		} else {
			retJson.put("msg", "未找到数据");
		}
		return retJson;
	}
	public SystemUser getLoginUser() {

		SystemUser user = null;
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
		QueryParam queryParam = new QueryParam();
		queryParam.getSqlMap().put("userAccount", CommUtil.null2String(userDetails.getUsername()));
		List<SystemUser> userList = dao.findList(queryParam);
		if(CollectionUtils.isNotEmpty(userList)) {
			user = userList.get(0);
		}
		return user;
	}

	public List<SystemUser> getExecutorList(long industryId, long streetId) {

		String hql = " from SystemUser o where o.deleteStatus = false and o.address.level = 4 and o.address = " + CommUtil.null2Long(streetId) + " " +
				" and EXISTS(select i from o.industrySet i where i.id = "+CommUtil.null2Long(industryId)+" ) " +
				" and EXISTS(select sr from o.systemRoleSet sr where sr.id in (1,2) )  ";
		List<SystemUser> userList = dao.findObjects(hql, null);
		return userList;
	}
}